package com.rightsidetech.http.controller.admin.level;

import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.rightsidetech.commom.enums.ModifyName;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.ModifyLog;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.AddLevelRuleReq;
import com.rightsidetech.system.dtos.request.UpdateLevelRuleReq;
import com.rightsidetech.system.dtos.response.LevelRuleResp;
import com.rightsidetech.system.dtos.response.LevelSearchResp;
import com.rightsidetech.system.service.LevelRuleService;
import com.rightsidetech.commom.base.PageRequest;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.List;

/**
 * @Package: com.rightsidetech.http.controller.admin.level
 * @ClassName: LecelRuleController
 * @Author: yinj
 * @CreateTime: 2021/10/20 10:41
 * @Description: 会员等级规则
 */
@RestController
@RequestMapping("/admin/level_rule")
@ShenyuSpringMvcClient(path = "/admin/level_rule/**")
@Api(tags = "会员等级相关api")
public class LecelRuleController extends BaseController {

    @Resource
    private LevelRuleService levelRuleService;


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/page")
    @ApiOperation("会员等级分页")
    public ResponseData<PageInfo<LevelRuleResp>> page(PageRequest request){
        ConstraintViolation validate = BeanValidator.validate(request);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return levelRuleService.page(request);

    }


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/add")
    @ApiOperation("新增")
    @ModifyLog(serviceClass = LevelRuleService.class,modifyType = ModifyName.SAVE)
    public ResponseData add(@RequestBody AddLevelRuleReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return levelRuleService.add(req,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/update")
    @ApiOperation("修改")
    @ModifyLog(serviceClass = LevelRuleService.class)
    public ResponseData update(@RequestBody UpdateLevelRuleReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return levelRuleService.update(req,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/del")
    @ApiOperation("删除")
    @ApiOperationSupport(ignoreParameters = {"level", "levelName","orderNum"})
    @ModifyLog(serviceClass = LevelRuleService.class)
    public ResponseData del(@RequestBody UpdateLevelRuleReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return levelRuleService.del(req,loginInfoId);
    }


    @Token
    @GetMapping("/search")
    @ApiOperation("会员等级下拉框查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "level",value = "会员等级 如:1",required = false)
    })
    public ResponseData<List<LevelSearchResp>> search(Byte level){
        return levelRuleService.search(level);

    }

}
